package yxy.game.pm2.active.command.celebration;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.acttreasurestepreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_国庆寻宝;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.NationalDayMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 国庆寻宝重置
 *
 * @ClassName CMD_Active_国庆寻宝_NationalDayLotteryReset
 * @Author jingtao-zheng
 * @Date 2022/9/27 21:00
 * @Version 1.0
 **/
@MessageType(proto = Command.NationalDayLotteryReset, classtype = NationalDayMessage.NationalDayLotteryResetRequest.class)
public class CMD_Active_国庆寻宝_NationalDayLotteryReset extends Command<NationalDayMessage.NationalDayLotteryResetRequest> {


    @Override
    public void execute(MessageSource messageSource, NationalDayMessage.NationalDayLotteryResetRequest request) throws Exception {
        String playercode = messageSource.getTag();
        NationalDayMessage.NationalDayLotteryResetResponse.Builder response = NationalDayMessage.NationalDayLotteryResetResponse.newBuilder();
        try {
            ActiveModule_国庆寻宝 am = ActiveModule_国庆寻宝.get(playercode, false);
            if (am.tryLock()) {
                try {
                    int floor = am.getFloor();
                    Map<String, acttreasurestepreward> allData = DataCenter.getAllData(acttreasurestepreward.class);
                    int asInt = allData.values().stream().mapToInt(acttreasurestepreward::getStep).max().getAsInt();
                    //层数不能小于策划配置的最高层
                    if(floor<asInt){
                        send(messageSource, response, Result.DataError);
                        return;
                    }
                    //1.层数重置为0
                    am.setFloor(Constant.ZERO);
                    //2.筛选出没领取的奖励
                    ArrayList<Integer> floorAward = am.getFloorAward();
                    List<Integer> allreward = allData.values().stream().map(acttreasurestepreward::getId).collect(Collectors.toList());
                    allreward.removeAll(floorAward);
                    Collection<Currency> currencies = new ArrayList<>();
                    for (Integer id : allreward) {
                        acttreasurestepreward acttreasurestepreward = allData.get(String.valueOf(id));
                        Collection<Currency> currencies1 = Currency.create(acttreasurestepreward.getRewardtype(), acttreasurestepreward.getRewardid(), acttreasurestepreward.getRewardnum());
                        currencies.addAll(currencies1);
                        Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(currencies1);
                        response.addAllLeftRewards(items);
                    }
                    //3.领取过得奖励记录清空 加上 格子要重置
                    am.getFloorAward().clear();
                    am.resetCell();
                    //4.状态前置,避免重复发奖
                    am.set();
                    //5.发奖
                    new CurrencyHandle().addCurrency(playercode, CCReason.国庆寻宝, currencies);
                    send(messageSource, response, Result.Success);
                } catch (Exception e) {
                    e.printStackTrace();
                    send(messageSource, response, Result.DataError);
                } finally {
                    am.unlock();
                }
            } else {
                send(messageSource, response, Result.DataError);
                return;
            }
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void send(MessageSource messageSource, NationalDayMessage.NationalDayLotteryResetResponse.Builder response, Result
            result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
